package com.haha.keymap;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

import java.awt.*;

/**
 * @author shenlinnan
 * @date 2025/7/6 1:55
 */
public class MousePointColorDetector implements NativeKeyListener {

    private static final int HOTKEY = NativeKeyEvent.VC_F11;
    private static volatile boolean f11Pressed = false;
    private static Robot robot;

    static {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        try {
            // 配置并注册全局键盘钩子
            GlobalScreen.registerNativeHook();
            GlobalScreen.addNativeKeyListener(new MousePointColorDetector());

            // 禁用JNativeHook日志
            java.util.logging.Logger.getLogger(GlobalScreen.class.getPackage().getName())
                    .setLevel(java.util.logging.Level.OFF);

            System.out.println("程序已启动，按 F11 获取鼠标位置和颜色信息");
            System.out.println("按 ESC 退出程序");

            // 保持程序运行
            Thread.currentThread().join();

        } catch (NativeHookException | InterruptedException ex) {
            ex.printStackTrace();
        } finally {
            try {
                GlobalScreen.unregisterNativeHook();
            } catch (NativeHookException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        if (e.getKeyCode() == HOTKEY) {
            f11Pressed = true;
            // 避免重复处理持续按键
        } else if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
            System.exit(0);
        }
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
        if (e.getKeyCode() == HOTKEY && f11Pressed) {
            f11Pressed = false;
            captureMouseData();
        }
    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {}

    private void captureMouseData() {
        try {
            // 获取鼠标位置信息
            Point mousePosition = MouseInfo.getPointerInfo().getLocation();
            int x = mousePosition.x;
            int y = mousePosition.y;

            // 捕获屏幕颜色
            Color color = robot.getPixelColor(x, y);

            // 格式化为十六进制颜色代码
            String hexColor = String.format("#%02X%02X%02X",
                    color.getRed(),
                    color.getGreen(),
                    color.getBlue());

            // 创建ASCII彩色显示
            String asciiColorBlock = "\033[48;2;" +
                    color.getRed() + ";" +
                    color.getGreen() + ";" +
                    color.getBlue() + "m  \033[0m";

            // 输出结果
            System.out.println("┌───────────────────────────────");
            System.out.printf("│ 位置: (%d, %d)%n", x, y);
            System.out.printf("│ RGB: %s (%d, %d, %d)%n", asciiColorBlock,
                    color.getRed(), color.getGreen(), color.getBlue());
            System.out.printf("│ HEX: %s%n", hexColor);
            System.out.println("└───────────────────────────────");

        } catch (Exception ex) {
            System.err.println("获取鼠标数据时出错: " + ex.getMessage());
        }
    }
}
